"""
作者：李俊明
时间：2021/10/19
版本：1.0
功能：定义字典保存员工通讯录并能够打印通讯录
"""

def print_all(dict):
	"""
	打印全部员工的通讯录
	:param dict_p: 字典
	:return: dict_p
	"""
	print('----------员工信息显示如下----------')
	for i in dict.items():
		print(i)

def print_byid(dict,id):
	"""
	打印指定员工的通讯录
	:param dict_p: 字典
	:param id: 员工id
	:return: dict_p
	"""
	try:
		print('----------员工信息显示如下----------')
		person_info = dict[id]
		# 字符串切片 列表
		person_list = person_info.split()
		name = person_list[0]
		tel = person_list[1]
		city = person_list[2]
		print('Id: {}, Name: {}, Tel: {}, City: {}'
			  .format(id, name, tel, city))
	except KeyError:
		print('需要打印的员工工号不存在！')
	except:
		print('程序发生未知异常，请联系软件工程师！')
def print_byname(dict_p,name):
	try:
		print('----------员工信息显示如下----------')
		id=get_key(dict_p,name)
		person_info = dict_p[id]
		# 字符串切片 列表
		person_list = person_info.split()
		name = person_list[0]
		tel = person_list[1]
		city = person_list[2]
		print('Id: {}, Name: {}, Tel: {}, City: {}'
			  .format(id, name, tel, city))
	except KeyError:
		print('需要打印的员工工号不存在！')
	except:
		print('程序发生未知异常，请联系软件工程师！')
def print_bytel(dict_p,tel):
	try:
		print('----------员工信息显示如下----------')
		id=get_key(dict_p,tel)
		person_info = dict_p[id]
		# 字符串切片 列表
		person_list = person_info.split()
		name = person_list[0]
		tel = person_list[1]
		city = person_list[2]
		print('Id: {}, Name: {}, Tel: {}, City: {}'
			  .format(id, name, tel, city))
	except KeyError:
		print('需要打印的员工工号不存在！')
	except:
		print('程序发生未知异常，请联系软件工程师！')
def print_bycity(dict_p,city):
	try:
		print('----------员工信息显示如下----------')
		id = get_key(dict_p, city)
		person_info = dict_p[id]
		# 字符串切片 列表
		person_list = person_info.split()
		name = person_list[0]
		tel = person_list[1]
		city = person_list[2]
		print('Id: {}, Name: {}, Tel: {}, City: {}'
			  .format(id, name, tel, city))
	except KeyError:
		print('需要打印的员工工号不存在！')
	except:
		print('程序发生未知异常，请联系软件工程师！')
"""def get_key (dict, value):
	return [k for k,v in dict.items() if v == value]
"""
def get_key(dict,name):
	for key in dict:
		if dict[key].__contains__(name):
			key_value=key
		return key_value
def del_byid(dict,id):
	del dict[id]
	print_all(dict)
def add(dict):
	key=input("请输入你要添加的员工id：")
	key_value=input("请输入员工信息：")
	dict['key']=key_value
	print_all(dict)
def main():
	dict_p={'1': '张晓 13800000000 武汉',
			'2': '李明 18500000000 北京',
			'3': '李浩 13912341234 九江',
			'4': '李明 15890281734 上海',
			'5': '张亮 15807191756 武汉'}
	msg=input("请选择你所需的功能：\n1.查找某员工的通讯录\n2.查找全部员工的通讯录\n3.删除某员工\n4.添加员工\n")
	if(msg=='1'):
		choose_value=input("请选择你所需要的查找方式：\n1.按员工ID查找\n2.按员工姓名查找\n3.按员工电话查找\n4.按员工城市查找\n")
		if(choose_value=='1'):
			id = input("请输入员工id：")
			print_byid(dict_p, id)
		elif(choose_value=='2'):
			name=input("请输入员工姓名：")
			print_byname(dict_p,name)
		elif(choose_value=='3'):
			tel=input("请输入员工电话：")
			print_bytel(dict_p,tel)
		elif(choose_value=='4'):
			city=input("请输入员工城市：")
			print_bycity(dict_p,city)
	elif(msg=='2'):
		print_all(dict_p)
	elif(msg=='3'):
		id=input("请输入员工的id：")
		del_byid(dict_p,id)
	else:
		if(msg=='4'):
			add(dict_p)
if __name__=='__main__':
	main()